﻿using System;
using System.Text;

using OST.Shared.Extensions;

namespace OST.Shared.SharedTypes
{
    /// <summary>
    /// represents the time-service (ntp) configuration
    /// </summary>
    public class TimeServiceConfiguration
    {
        #region Consts

        private const string C_STR_DEFAULTTIMESERVER = "132.246.168.148";
        #endregion

        #region CTOR

        public TimeServiceConfiguration()
        {
            UseNtp = false;
            TimeZone = 0;
            NtpServer = C_STR_DEFAULTTIMESERVER;
            Value = new DateTime(2014, 4, 18, 12, 15, 00);
        }
        #endregion

        #region Properties

        /// <summary>
        /// flag for using the time-server-property or not
        /// </summary>
        public bool UseNtp { get; set; }

        /// <summary>
        /// ntp timer-server 
        /// </summary>
        /// <remarks>default: time.nrc.ca</remarks>
        public string NtpServer { get; set; }

        /// <summary>
        /// time zone
        /// </summary>
        /// <remarks>
        /// -+1 = Berlin + 1  summer time -> +2
        /// </remarks>
        public int TimeZone { get; set; }

        /// <summary>
        /// manual timestamp value, used, if UseTimeServer-Flag is False
        /// </summary>
        public DateTime Value { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// validates the this instance
        /// </summary>
        public void Validate()
        {
            if (UseNtp && NtpServer.IsNullOrEmpty())
                throw new OSTException("if ntp is used, the ntp-server has to be specified");
        }

        public override string ToString()
        {
            var str = new StringBuilder();

            str.AppendLine();
            str.AppendLine("UseNtp: " + UseNtp);
            str.AppendLine("NtpServer: " + NtpServer);
            str.AppendLine("TimeZone: " + TimeZone);
            str.AppendLine("Value: " + Value);

            return str.ToString();
        }

        #endregion
    }
}
